package cn.com.xinjinfu.pdoulcProduct.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.entity.ReturnedMoneyBean;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.utils.NewUtil;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;

/**
 * Created by android-adt on 2016/4/20.
 */
public class ReturnedMoneyListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;
    private List<ReturnedMoneyBean.ReceiveList> list;

    public void setList(List<ReturnedMoneyBean.ReceiveList> slist) {
        list.clear();
        list.addAll(slist);
        notifyDataSetChanged();
    }

    /**
     * 分页数据加载到listview
     */
    public void addList(List slist) {
        list.addAll(slist);
        notifyDataSetChanged();
    }

    public ReturnedMoneyListAdapter(Context context) {
        list = new ArrayList<>();
        this.context=context;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_returned_money, null);
            holder = new ViewHolder();
            holder.rl_bg = (RelativeLayout) convertView.findViewById(R.id.rl_bg);
            holder.tv_year = (TextView) convertView.findViewById(R.id.tv_year);
            holder.tv_month = (TextView) convertView.findViewById(R.id.tv_month);
            holder.has_resiver = (TextView) convertView.findViewById(R.id.has_resiver);
            holder.will_resiver = (TextView) convertView.findViewById(R.id.will_resiver);
            convertView.setTag(holder);//绑定ViewHolder对象
        } else {
            holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
        }
        ReturnedMoneyBean.ReceiveList receiveList = list.get(position);
        RelativeLayout.LayoutParams Params = (RelativeLayout.LayoutParams) holder.rl_bg.getLayoutParams(); // 取控件mGrid当前的布局参数
        if (receiveList != null) {
            String date = receiveList.getReceive_month();
            int years = Integer.parseInt(date.substring(0, date.indexOf("-")));
//            int month = Integer.parseInt(date.substring(date.indexOf("-") + 1, date.indexOf("-") + 3));
            String month01 = date.substring(date.indexOf("-") + 1, date.indexOf("-") + 3);
            holder.tv_year.setText(years + "");
            holder.tv_month.setText(month01 + "");
            holder.has_resiver.setText("已还 " + TextViewUtil.getSumLength(receiveList.getHas_reveived()));
            holder.will_resiver.setText("待还 " + TextViewUtil.getSumLength(receiveList.getWill_receive()));
            if (receiveList.getHas_reveived() > 0 && receiveList.getWill_receive() > 0) {
                holder.rl_bg.setBackgroundColor(0xff6ED3D6);
                Params.height = NewUtil.dip2px(context,80);// 当控件的高强制设成80象素
                holder.rl_bg.setLayoutParams(Params); // 使设置好的布局参数应用到控件myGrid
                holder.has_resiver.setVisibility(View.VISIBLE);
                holder.will_resiver.setVisibility(View.VISIBLE);
            } else if (receiveList.getHas_reveived() > 0) {
                holder.rl_bg.setBackgroundColor(0xff6ED3D6);
                Params.height = NewUtil.dip2px(context,60);
                holder.rl_bg.setLayoutParams(Params);
                holder.has_resiver.setVisibility(View.VISIBLE);
                holder.will_resiver.setVisibility(View.GONE);
            } else if (receiveList.getWill_receive() > 0) {
                holder.rl_bg.setBackgroundColor(0xffF4B942);
                Params.height = NewUtil.dip2px(context,60);
                holder.rl_bg.setLayoutParams(Params);
                holder.has_resiver.setVisibility(View.GONE);
                holder.will_resiver.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }
    class ViewHolder {
        public RelativeLayout rl_bg;
        public TextView tv_year;
        public TextView tv_month;
        public TextView has_resiver;
        public TextView will_resiver;
    }

}



